package com.john.booter.module.role.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.transaction.Transactional;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.john.booter.domain.entity.Option;
import com.john.booter.domain.entity.Role;
import com.john.booter.exception.RecordNotFoundException;
import com.john.booter.module.option.dao.OptionDao;
import com.john.booter.module.role.dao.RoleDao;
import com.john.booter.module.role.service.RoleService;
import com.john.booter.pagination.DataTable;
import com.john.booter.pagination.Pager;

@Service
public class RoleServiceImpl implements RoleService {
	
	@Autowired
	private RoleDao dao;
	@Autowired
	private OptionDao optionDao;

	@Transactional
	@Override
	public void add(Role r) {
		dao.add(r);
	}

	@Transactional
	@Override
	public void delete(String code) {
		dao.delete(code);
	}
	
	@Transactional
	@Override
	public void batchDelete(List<String> codes) {
		dao.batchDelete(codes);
	}

	@Transactional
	@Override
	public void update(Role r) {
		dao.update(r);
	}

	@Override
	public List<Role> findAll() {
		return dao.findAll();
	}

	@Override
	public Role findByCode(String code) {
		return dao.findByCode(code);
	}

	@Override
	public DataTable<Role> findDataTable(Pager pg, String name) {
		return dao.findDataTable(pg, name);
	}
	
	@Transactional
	@Override
	public void updateRoleOptions(String roleCode, String optionCodes) {
		Role role = dao.findByCode(roleCode);
		if(role == null) {
			throw new RecordNotFoundException(String.format("No %s entity with id %s exists!", Role.class.getName(), roleCode));
		}
		
		Set<Option> options = new HashSet<Option>();
		if (StringUtils.isNotEmpty(optionCodes) && !optionCodes.equals("null")) {
			String[] codes = optionCodes.split(",");
			List<Option> list = optionDao.findByCodes(codes);
			if(CollectionUtils.isNotEmpty(list)) {
				options = new HashSet<Option>(list);
			}
		} 	
		role.setOptions(options);
		dao.update(role);
	}
	
	@Override
	public String getRoleOptionsTree(String roleCode) {
		List<Option> all = optionDao.findAll();
		Set<Option> options = new HashSet<Option>();
		Role role = dao.findByCode(roleCode);
		
		if(role != null) {
			options = role.getOptions();
		}
		
		StringBuilder sb = new StringBuilder("[");
		for (Option allOption : all) {
			sb.append("{\"id\":\"").append(allOption.getCode())
			  .append("\",\"pId\":\"").append(allOption.getParentCode())
			  .append("\", \"name\":\"").append(allOption.getName())
			  .append("\", \"open\":\"false\"");
			if(CollectionUtils.isNotEmpty(options)) {
				for (Option opt : options) {
					if (StringUtils.isNotEmpty(allOption.getCode()) && (allOption.getCode().equals(opt.getCode()))) {
						sb.append(",\"checked\":\"true\"");
					}
				}
			}
			sb.append("},");
		}
		String str = sb.substring(0, sb.lastIndexOf(","));
		str+="]";
		
		return str;
	}
	
}
